﻿// Truncator.cs
//

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace BookClub.Core {

    public sealed class Truncator : IValueConverter {

        public int MaxWords {
            get;
            set;
        }

        #region Implementation of IValueConverter
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string text = (string)value;
            if (String.IsNullOrEmpty(text)) {
                return text;
            }

            string[] words = text.Split(new char[] { ' ', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries);

            text = String.Join(" ", words, 0, Math.Min(MaxWords, words.Length));
            if (words.Length > MaxWords) {
                text = text + " ...";
            }

            return text;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
        #endregion
    }
}
